#include "inspectghostssubdialog.h"
#include "ui_inspectghostssubdialog.h"

#include "global.h"

#include <QSqlQuery>
#include <QDebug>


InspectGhostSSubDialog::InspectGhostSSubDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::InspectGhostSSubDialog)
{
    ui->setupUi(this);
}

InspectGhostSSubDialog::~InspectGhostSSubDialog()
{
    delete ui;
}

void InspectGhostSSubDialog::on_btnInspect_clicked()
{
    //检查“幽灵”子目
    QSqlQuery q;
    QSet<int> ghostSids;
    if(!ui->chkBusi->isChecked() || !ui->chkExtra->isChecked())
        return;
    QString s;
    QList<int> ids = allSndSubs.keys();
    int sid;

    //1、首先查分录表
    QTableWidgetItem* item;
    if(ui->chkBusi->isChecked()){
        ui->twBusi->setRowCount(0);
        s = QString("select id,pid,firSubId,secSubId,jMoney,dMoney,dir from BusiActions");
        if(!q.exec(s))
            return;
        int row=0;
        while(q.next()){
            sid = q.value(3).toInt();
            if(sid == 0)
                continue;
            if(ids.contains(sid))
                continue;
            ghostSids.insert(sid);
            int pid = q.value(1).toInt();
            int fid=q.value(2).toInt();
            int bid = q.value(0).toInt();
            int dir = q.value(6).toInt();
            Double v;
            if(dir == DIR_J)
                v = Double(q.value(4).toDouble());
            else
                v = Double(q.value(5).toDouble());
            QSqlQuery q1;
            s = QString("select date,number from PingZhengs where id=%1").arg(pid);
            if(!q1.exec(s))
                return;
            q1.first();
            QString date = q1.value(0).toString();
            int pzNum = q1.value(1).toInt();
            ui->twBusi->insertRow(row);
            ui->twBusi->setItem(row,0,new QTableWidgetItem(date));
            ui->twBusi->setItem(row,1,new QTableWidgetItem(QString::number(pzNum)));
            ui->twBusi->setItem(row,2,new QTableWidgetItem(QString::number(fid)));
            item = new QTableWidgetItem(QString::number(sid));
            item->setData(Qt::UserRole,bid);
            ui->twBusi->setItem(row,3,item);
            ui->twBusi->setItem(row,4,new QTableWidgetItem(v.toString2()));
            row++;
        }
    }
    if(ui->chkExtra->isChecked()){
        //2、查原币余额表
        ui->twExtra->setRowCount(0);
        QList<int> ps,ys,ms,mts;
        s = QString("select id,year,month,mt from subjectExtras");
        if(!q.exec(s))
            return;
        while(q.next()){
            ps<<q.value(0).toInt();
            ys<<q.value(1).toInt();
            ms<<q.value(2).toInt();
            mts<<q.value(3).toInt();
        }
        s = QString("select * from detailExtras");
        if(!q.exec(s))
            return;
        int row = 0;
        while(q.next()){
            int sid = q.value(2).toInt();
            if(ids.contains(sid))
                continue;
            ghostSids.insert(sid);
            int seid = q.value(1).toInt();
            int index = ps.indexOf(seid);
            if(index == -1)
                return;
            int eid = q.value(0).toInt();
            Double v = Double(q.value(4).toDouble());
            int dir = q.value(3).toInt();
            ui->twExtra->insertRow(row);
            ui->twExtra->setItem(row,0,new QTableWidgetItem(mts.at(index)==RMB?tr("人民币"):tr("美金")));
            ui->twExtra->setItem(row,1,new QTableWidgetItem(QString::number(ys.at(index))));
            ui->twExtra->setItem(row,2,new QTableWidgetItem(QString::number(ms.at(index))));
            ui->twExtra->setItem(row,3,new QTableWidgetItem(v.toString2()));
            ui->twExtra->setItem(row,4,new QTableWidgetItem(dirStr(dir)));
            item = new QTableWidgetItem(QString::number(sid));
            item->setData(Qt::UserRole,eid);
            ui->twExtra->setItem(row,5,item);
            row++;
        }

        //3、查本币余额表
        ps.clear();ys.clear();ms.clear();mts.clear();
        s = QString("select id,year,month from subjectMmtExtras");
        if(!q.exec(s))
            return;
        while(q.next()){
            ps<<q.value(0).toInt();
            ys<<q.value(1).toInt();
            ms<<q.value(2).toInt();
        }
        s = QString("select * from detailMmtExtras");
        if(!q.exec(s))
            return;
        while(q.next()){
            int sid = q.value(2).toInt();
            if(ids.contains(sid))
                continue;
            ghostSids.insert(sid);
            int seid = q.value(1).toInt();
            int index = ps.indexOf(seid);
            if(index == -1)
                return;
            int eid = q.value(0).toInt();
            Double v = Double(q.value(3).toDouble());
            ui->twExtra->insertRow(row);
            ui->twExtra->setItem(row,0,new QTableWidgetItem(tr("美金本币")));
            ui->twExtra->setItem(row,1,new QTableWidgetItem(QString::number(ys.at(index))));
            ui->twExtra->setItem(row,2,new QTableWidgetItem(QString::number(ms.at(index))));
            ui->twExtra->setItem(row,3,new QTableWidgetItem(v.toString2()));
            item = new QTableWidgetItem(QString::number(sid));
            item->setData(Qt::UserRole,eid);
            ui->twExtra->setItem(row,5,item);
        }
//        if(!ghostSids.isEmpty()){
//            s = tr("发现的幽灵子目是");
//            foreach(int sid, ghostSids.values()){
//                s.append(QString("%1,").arg(sid));
//            }
//            s.chop(1);
//            qDebug()<<s;
//        }
    }





}

/**
 * @brief 清除余额表中引用幽灵子目的余额条目
 */
void InspectGhostSSubDialog::on_btnClearInExtra_clicked()
{
    //如果未选择，则清除所有条目，否则清除所选条目
    if(ui->twExtra->rowCount() == 0)
        return;
    if(QMessageBox::No == QMessageBox::warning(this,"",tr("移除后将不可恢复，确定吗？"),
                                               QMessageBox::Yes|QMessageBox::No,
                                               QMessageBox::No))
        return;
    QSqlQuery q;
    QList<QTableWidgetSelectionRange> ranges = ui->twExtra->selectedRanges();
    QList<int> rows;
    if(ranges.isEmpty()){
        for(int i = 0; i < ui->twExtra->rowCount(); ++i)
            rows<<i;
    }
    else{
        foreach(QTableWidgetSelectionRange range,ranges){
            for(int i = range.topRow(); i <=range.bottomRow(); ++i)
                rows<<i;
        }
        qSort(rows.begin(),rows.end());
    }
    for(int i = rows.count()-1; i >= 0; i--){
        QString table;
        if(ui->twExtra->item(i,0)->text() == tr("美金本币"))
            table = "detailMmtExtras";
        else
            table = "detailExtras";
        int eid = ui->twExtra->item(i,5)->data(Qt::UserRole).toInt();
        QString s = QString("delete from %1 where id=%2").arg(table).arg(eid);
        if(!q.exec(s))
            return;
        ui->twExtra->removeRow(i);
    }
}
